package com.apimarket.config;

import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import lombok.Data;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @Description:
 * @Author ZXR
 * @Date 2021/7/16 20:36
 * @Version 1.0
 */

//@Data
//@Configuration
//@MapperScan(basePackages = MaterDataBaseConfig.PACKAGE ,sqlSessionFactoryRef = "masterSqlSessionFactory")
public class AtomikosMasterDBConfig {
    /**
     * Dao层包路径
     * 这里加private会爆红
     */
    static final String PACKAGE="com.apimarket.dao";

    /**
     * mapper文件的相对路径
     */
    private static final String MAPPER_LOCATION="classpath*:mapper/master/*.xml";

    //用AtomikosDataSourceBean来统一包装数据源
    @Primary
    @Bean("atomikosMasterDataSourcProperties")
    @ConfigurationProperties(prefix = "spring.datasource.druid.atomikos.master")
    public DataSourceProperties masterDataSourceProperties(){
        return new DataSourceProperties();
    }

    @Primary
    @Bean(name="atomikosMasterDataSource")
    @DependsOn({"xaTransaction"})
    public DataSource primaryDataSource(@Qualifier("atomikosMasterDataSourcProperties")DataSourceProperties dataSourceProperties){
        //这里mysql-connect的版本有要求，否则可能会找不到这个类
        MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        mysqlXADataSource.setUrl(dataSourceProperties.getUrl());
        mysqlXADataSource.setPinGlobalTxToPhysicalConnection(true);
        mysqlXADataSource.setPassword(dataSourceProperties.getPassword());
        mysqlXADataSource.setUser(dataSourceProperties.getUsername());
        mysqlXADataSource.setPinGlobalTxToPhysicalConnection(true);

        AtomikosDataSourceBean ds=new AtomikosDataSourceBean();
        ds.setXaDataSource(mysqlXADataSource);
        ds.setUniqueResourceName("atomikosMasterDataSource");
        ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
        ds.setPoolSize(10);
        return ds;
    }

    //创建Mybatis的连接会话工厂实例
    @Primary
    @Bean(name = "atomikosMasterSqlSessionFactory")
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("atomikosMasterDataSource") DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);  // 设置数据源bean
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(AtomikosMasterDBConfig.MAPPER_LOCATION));  // 设置mapper文件路径

        return sessionFactory.getObject();
    }

}
